package com.atguigu.tingshu.album.service.impl;

import com.atguigu.tingshu.album.mapper.AlbumAttributeValueMapper;
import com.atguigu.tingshu.album.mapper.AlbumInfoMapper;
import com.atguigu.tingshu.album.mapper.AlbumStatMapper;
import com.atguigu.tingshu.album.service.AlbumInfoService;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.rabbit.constant.MqConst;
import com.atguigu.tingshu.common.rabbit.service.RabbitService;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.AlbumAttributeValue;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.AlbumStat;
import com.atguigu.tingshu.query.album.AlbumInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumAttributeValueVo;
import com.atguigu.tingshu.vo.album.AlbumInfoVo;
import com.atguigu.tingshu.vo.album.AlbumListVo;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Executors;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class AlbumInfoServiceImpl extends  ServiceImpl<AlbumInfoMapper, AlbumInfo> implements AlbumInfoService {

	@Autowired
	private AlbumInfoMapper albumInfoMapper;
    @Autowired
    private AlbumAttributeValueMapper albumAttributeValueMapper;
    @Autowired
    private AlbumStatMapper albumStatMapper;
    @Autowired
    private RabbitService rabbitService;
    /**
     * @author gengjian
     * @date 2025/02/21 15:54:45
     * 分页查询用户专辑列表
     */
    @Override
    public void findUserAlbumPage(Page<AlbumListVo> page, AlbumInfoQuery albumInfoQuery) {
        //AlbumListVo我们需要的列表数据包括专辑信息和专辑对应的统计信息 写sql比较方便所以调用mapper层
        albumInfoMapper.selectUserAlbumPage(page,albumInfoQuery);
    }

    /**
     * @author gengjian
     * @date 2025/02/19 15:17:48
     * 保存专辑
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveAlbumInfo(AlbumInfoVo albumInfoVo,Long userId) {
        //保存专辑信息
        AlbumInfo albumInfo = new AlbumInfo();
        //拷贝属性 缺少status状态 用户id 专辑评分 免费试听集数
        BeanUtils.copyProperties(albumInfoVo,albumInfo);
        albumInfo.setUserId(userId);
        //判断专辑类型是否是免费 如果付费则设置免费试听集数
        if (!SystemConstant.ALBUM_PAY_TYPE_FREE.equals(albumInfoVo.getPayType())){
            albumInfo.setTracksForFree(5);
        }
        albumInfo.setStatus(SystemConstant.ALBUM_STATUS_PASS);
        albumInfoMapper.insert(albumInfo);
        //保存专辑属性关系表信息
        List<AlbumAttributeValueVo> albumAttributeValueVoList = albumInfoVo.getAlbumAttributeValueVoList();
        if (!CollectionUtils.isEmpty(albumAttributeValueVoList)){
            for (AlbumAttributeValueVo albumAttributeValueVo : albumAttributeValueVoList) {
                AlbumAttributeValue albumAttributeValue = new AlbumAttributeValue();
                BeanUtils.copyProperties(albumAttributeValueVo,albumAttributeValue);
                albumAttributeValue.setAlbumId(albumInfo.getId());
                albumAttributeValueMapper.insert(albumAttributeValue);
            }
        }
        //保存专辑统计
        saveAlbumStat(albumInfo.getId(),SystemConstant.ALBUM_STAT_PLAY);
        saveAlbumStat(albumInfo.getId(),SystemConstant.ALBUM_STAT_SUBSCRIBE);
        saveAlbumStat(albumInfo.getId(),SystemConstant.ALBUM_STAT_BROWSE);
        saveAlbumStat(albumInfo.getId(),SystemConstant.ALBUM_STAT_COMMENT);
        //如果是公开则上架
        if ("1".equals(albumInfoVo.getIsOpen())){
            //发送上架消息
            rabbitService.sendMessage(MqConst.EXCHANGE_ALBUM,MqConst.ROUTING_ALBUM_UPPER,albumInfo.getId());
        }
    }

    //改了修饰符为公开，私有的会导致spring事务失效
    public void saveAlbumStat(Long id, String albumStatComment) {
        AlbumStat albumStat = new AlbumStat();
        albumStat.setAlbumId(id);
        albumStat.setStatType(albumStatComment);
        albumStat.setStatNum(new Random().nextInt(1000000));
        albumStatMapper.insert(albumStat);
    }

    /**
     * @author gengjian
     * @date 2025/02/21 18:39:16
     * 根据专辑id删除专辑
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeAlbumInfo(Long albumId) {
        //根据专辑id删除专辑信息 逻辑删除
        albumInfoMapper.deleteById(albumId);
        //逻辑删除专辑属性关联表
        LambdaQueryWrapper<AlbumAttributeValue> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AlbumAttributeValue::getAlbumId,albumId);
        albumAttributeValueMapper.delete(lambdaQueryWrapper);
        //删除专辑统计
        LambdaQueryWrapper<AlbumStat> albumStatLambdaQueryWrapper = new LambdaQueryWrapper<>();
        albumStatLambdaQueryWrapper.eq(AlbumStat::getAlbumId,albumId);
        albumStatMapper.delete(albumStatLambdaQueryWrapper);
        //下架
        rabbitService.sendMessage(MqConst.EXCHANGE_ALBUM, MqConst.ROUTING_ALBUM_LOWER, albumId);
    }

    /**
     * @author gengjian
     * @date 2025/02/21 19:07:14
     * 根据专辑id修改专辑
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateAlbumInfo(Long albumId,AlbumInfoVo albumInfoVo) {
        //先修改专辑信息
        AlbumInfo albumInfo = new AlbumInfo();
        //属性拷贝
        BeanUtils.copyProperties(albumInfoVo, albumInfo);
        //缺少专辑id
        albumInfo.setId(albumId);
        albumInfoMapper.updateById(albumInfo);
        //再逻辑删除专辑属性关系表再新增
        albumAttributeValueMapper.delete(
                new LambdaQueryWrapper<AlbumAttributeValue>().eq(AlbumAttributeValue::getAlbumId, albumId));
        List<AlbumAttributeValueVo> albumAttributeValueVoList = albumInfoVo.getAlbumAttributeValueVoList();
        if (!CollectionUtils.isEmpty(albumAttributeValueVoList)) {
            for (AlbumAttributeValueVo albumAttributeValueVo : albumAttributeValueVoList) {
                AlbumAttributeValue albumAttributeValue = new AlbumAttributeValue();
                BeanUtils.copyProperties(albumAttributeValueVo, albumAttributeValue);
                albumAttributeValue.setAlbumId(albumId);
                albumAttributeValueMapper.insert(albumAttributeValue);
            }
        }
        //如果公开则上架专辑如果私密则下架专辑
        if ("1".equals(albumInfoVo.getIsOpen())){
            //上架 需要album_id
            rabbitService.sendMessage(MqConst.EXCHANGE_ALBUM,MqConst.ROUTING_ALBUM_UPPER,albumId);
        }else{
            //下架 需要album_id
            rabbitService.sendMessage(MqConst.EXCHANGE_ALBUM,MqConst.ROUTING_ALBUM_LOWER,albumId);
        }
    }
    /**
     * @author gengjian
     * @date 2025/02/21 19:21:52
     * //点击编辑先回显数据
     */
    @Override
    public AlbumInfo getAlbumInfo(Long albumId) {
        //根据id获取专辑信息
        AlbumInfo albumInfo = albumInfoMapper.selectById(albumId);
        //封装专辑对应属性值列表
        if (!ObjectUtils.isEmpty(albumInfo)){
            LambdaQueryWrapper<AlbumAttributeValue> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(AlbumAttributeValue::getAlbumId,albumId);
            albumInfo.setAlbumAttributeValueVoList(albumAttributeValueMapper.selectList(lambdaQueryWrapper));
        }
        return albumInfo;
    }

    /**
     * @author gengjian
     * @date 2025/02/26 18:31:34
     * 根据专辑id获取统计信息
     */
    @Override
    public AlbumStatVo getAlbumStatVo(Long albumId) {
        //调用mapper层
        AlbumStatVo albumStatVo = albumStatMapper.getAlbumStatVo(albumId);
        return albumStatVo;
    }
}
